<template>
	<view class="container-box">
		<Home v-if="activeTab==='home'"></Home>
		<Calendar v-if="activeTab==='record'"></Calendar>
		<My v-if="activeTab==='my'"></My>
		<view class="tabs-box display-flex">
			<view class="tab item" @click="onTab('home')">
				<image class="img" :src="activeTab==='home'?'/static/004.png':'/static/001.png'" />
				<view class="">
					首页
				</view>
			</view>
			<view class="tab item" @click="onTab('record')">
				<image class="img" :src="activeTab==='record'?'/static/005.png':'/static/002.png'" />
				<view class="">
					送奶记录
				</view>
			</view>
			<view class="tab item" @click="onTab('my')">
				<image class="img" :src="activeTab==='my'?'/static/006.png':'/static/003.png'" />
				<view class="">
					我的
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		watch
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import utils from '/common/util.js'
	import {
		globalData
	} from '/common/globalData.js'
	import Home from './home.vue'
	import Calendar from './calendar.vue'
	import My from './my.vue'
	const activeTab = ref('home')
	// const show = ref(false)
	const onTab = (name) => {
		const userInfo = getApp().globalData.userInfo
		if (name !== 'home' && (!userInfo || !userInfo.userId || !userInfo.addressCode || !userInfo
				.phone)) {

			uni.showModal({
				title: '您的用户信息还未完善，请先完善您的信息',
				content: '',
				confirmText: '去完善',
				success: function(res) {
					if (res.confirm) {
						console.log('用户点击确定');
						uni.navigateTo({
							url: '/pages_other/setting'
						})
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		} else {
			activeTab.value = name
		}
	}
	const getCookie = (name) => {
		if (name && document.cookie.length > 0) {
			console.log(document.cookie)
			var arr = document.cookie.split('; ');
			console.log(arr)
			for (var i = 0; i < arr.length; i++) {
				var arr2 = arr[i].split('='); //再次切割
				//判断查找相对应的值
				if (arr2[0] == name) {
					return arr2[1]; //cookie中的userName
				}
			}
		}
		return null
	}
	// 微信小程序-用户授权
	const getAuthCode = () => {
		uni.getProvider({
			service: 'oauth',
			success: (res) => {
				console.log('getAuthCode——————', res.provider);
				if (~res.provider.indexOf('weixin')) {
					uni.login({
						provider: 'weixin',
						success: (loginRes) => {
							console.log('login——————', JSON.stringify(loginRes));
							login(loginRes.code);
						}
					});
				}
			}
		});
	}

	// 微信小程序-登录或注册
	const login = (wxCode) => {
		const param = {
			code: wxCode,
			// 1 微信 2 支付宝
			// timestamp: util.getTimestamp()
		};
		console.log(param);
		// 登录注册
		getApp().globalData.serviceApi(param, 'login', 'GET').then(res => {
			console.log('login 结果：');
			console.log(res);
			uni.hideLoading();
			// 结果异常
			if (res.code != 0 || !res.data) {
				this.errMsg = res.message;
				return;
			}
			utils.saveToken(res.data.Authorization)
			getBaseUserInfo()
		})
	}
	// 测试-获取用户信息
	const getUserInfoTest = () => {
		getApp().globalData.userInfo = {
			"userId": "1", // 人员编号
			"name": "管理员", // 名称
			"phone": '18339921704', // 电话
			"openId": '', //openid
			"visitor": true, //是否游客：false 否， true 是
			"balance": 0, //余额
			"addressCode": 0, //地址编号
			"addressDesc": '比较好吧结婚不结婚就回家户籍科户籍科', //详细地址
			"deptId": "1", // 部门编号}
		}
	}
	// H5-登录或注册
	const getUserInfo = (code) => {
		getApp().globalData.serviceApi({
			code
		}, 'login', 'GET').then(res => {
			// getApp().globalData.userInfo = res.data
			utils.saveToken(res.data.Authorization)
			getBaseUserInfo()
		})
	}
	// 获取基础用户信息
	const getBaseUserInfo = () => {
		getApp().globalData.serviceApi({}, 'getBaseUserInfo', 'POST').then(res => {
			getApp().globalData.userInfo = res.data
			// utils.saveToken(res.data.Authorization)
		})
	}
	onLoad(() => {
		getApp().globalData = globalData
		console.log('@@@@@@@@@@@@@@@@@@22', getApp().globalData)
		// #ifdef MP-WEIXIN || MP-ALIPAY
		getAuthCode()
		// #endif
		// #ifdef H5
		const token = ''
		// const token =
		// 	'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJkYXRhc2NvcGUiOiIxIiwiZXhwIjoxNzExMDEwMTc3LCJpZGVudGl0eSI6Mzk5NTQsIm5pY2UiOiJjczAwNyIsIm9yaWdfaWF0IjoxNzExMDA2NTc3LCJyb2xlaWQiOi00LCJyb2xla2V5IjoibW9iaWxlX21nbXQiLCJyb2xlbmFtZSI6Iuenu-WKqOerr-euoeeQhuWRmCJ9.AMgdH8uqXsRd42ShjzhgoueqNWwa8B79HshrOXwKPLpgnmKwoEIo-DKcUJTdWQts07Fb99pAyW9p97JrR8-nQQ'
		// const token = getCookie('jwt')
		let code = utils.search('code')
		if (token) {
			// getBaseUserInfo()
			getUserInfoTest()
		} else if (code) {
			getUserInfo(code)
		}
		// #endif
	})
</script>

<style lang="less" scoped>
	.container-box {
		width: 100%;
		height: 100%;
		padding-bottom: 120rpx;
		overflow-y: scroll;

		.tabs-box {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 120rpx;
			font-size: 28rpx;
			color: #333;
			background-color: #fff;
			border-top: 1px solid #e5e5e5;

			.tab {
				width: 30%;
				text-align: center;

				.img {
					width: 64rpx;
					height: 56rpx;
				}
			}
		}
	}
</style>